// pages/appointmentInformation/appointmentInformation.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */

  data: {
    myHttp: app.globalData.myHttp,
    qiaokouAppointmentInfo: '', //前一页面传入的选择医生的信息
    appointmentDayTimeNum: '', //0下午，1上午
    scheduleId: "", ///////////////////前一个页面传入
    parttimeId: "", //准确预约时间的id
    hospitalid: "", /////////////////////////前一个页面传入
    appointmentTime: '', //前一页面传入的选择的预约日期的信息
    // visitorList:app.globalData.visitorList,
    visitorNameList: [],
    personIndex: 0,
    diagnosticStatus: 0,
    time: 0,
    appointmentPeriod: [],
    price: '', //订单价格
    familyId: "",
    userID: "",
    outPatient: "", //门诊类型
    weken: "", //星期几
    periodTime: "", //上午或下午
    orderInformation: {
      orderType: '', //订单类型
      orderId: '', //订单编号
      orderTime: '', //订单时间
      orderHospital: '', //订单医院
      orderDepartment: '', //订单科室
      orderDoctor: '', //订单医生
      orderVisitor: '', //订单的就诊人
      visitTime: '', //就诊时间
      telephone: '', //手机号码
      userID: '', //身份证号
      payAmount: '', //支付金额
      payType: '', //支付方式
    },

    changeTwo: [
      "初诊", "复诊"
    ],

  },

  //选择就诊人
  bindPickChangeOne(e) {
    console.log("改变就诊人为：", e.detail.value)
    this.setData({
      personIndex: e.detail.value
    })
  },

  //选择初诊/复诊
  bindPickChangeTwo(e) {
    console.log("改变初诊/复诊为：", e.detail.value)
    this.setData({
      diagnosticStatus: e.detail.value
    })
  },

  //选择就诊时间
  bindPickChangeThree(e) {
    console.log("改变就诊时间为：", e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },

  //用户点击弹窗中的确定后生成订单
  bindViewTap: function(event) {
    wx.showLoading({
      title: '预约中',
    })
    var that = this
    var orderInf = this.data.orderInformation
    var order_ID //查询订单时使用的order_ID
    var orderid //取消订单时使用的orderid
    var hospitalid = that.data.qiaokouAppointmentInfo.scheduledList.hospitalid //////////////////////////////前一个页面传入
    var currentUser = wx.getStorageSync("user")
    console.log(that.data.userID)

    // console.log(that.data.visitorList[that.data.personIndex].familyID)

    var index = that.data.personIndex;
    if (that.data.visitorList[index] != null && that.data.visitorList[index] != '' && that.data.visitorList[index] != undefined) {
      wx.request({
        url: that.data.myHttp + '/fuyou/createOrder',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
            userId: currentUser.userID,
            familyId: that.data.visitorList[index].familyID,
            scheduleId: that.data.qiaokouAppointmentInfo.scheduledList.scheduleid,
            parttimeId: that.data.qiaokouAppointmentInfo.scheduledList.list[that.data.time].parttimeid,
            hospitalid: hospitalid,
        },
        success(res) {
          wx.hideLoading()
          console.log(res.data)
          if (res.data.success) {
            order_ID = res.data.obj.id
            orderid = res.data.obj.orderId
            that.setData({
              orderInformation: orderInf,
              order_ID: order_ID

            })
            wx.redirectTo({
              url: '../orderdetails/orderdetails?order_ID=' + order_ID + '&hospitalId=' + hospitalid
            })
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel:false
            })
          }
        },
        fail() {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '新建订单失败！',
            showCancel:false
          })
        }
      })
    } else {
      wx.hideLoading()
      wx.showToast({
        title: '请选择就诊人',
        icon: 'none',
        duration: 2000
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var user = wx.getStorageSync("user")
    console.log(decodeURIComponent(options.qiaokouAppointmentInfo))
    var qiaokouAppointmentInfo = JSON.parse(decodeURIComponent(options.qiaokouAppointmentInfo))
    var appointmentPeriod = [];
    for (let i in qiaokouAppointmentInfo.scheduledList.list) {
      appointmentPeriod.push(qiaokouAppointmentInfo.scheduledList.list[i].starttime + '--' + qiaokouAppointmentInfo.scheduledList.list[i].endtime)
    }
    that.setData({
      userID: user.userID,
      qiaokouAppointmentInfo : qiaokouAppointmentInfo,
      appointmentPeriod: appointmentPeriod
    })
  },

  getPatients:function(){
    var that = this
    wx.request({
      url: this.data.myHttp + '/family/getFamilyPage',
      method: "GET",
      header: {
        'content-type': 'application/json',
      },
      data: {
        "userID": this.data.userID
      },
      success(res) {
        var visitorNameList = []
        var visitorList = res.data.obj.records
        var defaultPersonIndex = 0
        for (let i in visitorList) {
          visitorNameList.push(res.data.obj.records[i].familyName)
          if(visitorList[i].isDefault == 1){
            defaultPersonIndex = i
          }
        }
        that.setData({
          visitorList: visitorList,
          visitorNameList: visitorNameList,
          personIndex: defaultPersonIndex
        })
      }
    })
  },

  //添加就诊人
  addVisitor() {
    wx.navigateTo({
      url: '../patient/patient?isReport=true'
    })
  },


  //点击生成订单弹出对话框
  modalcnt: function() {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认生成订单？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.bindViewTap()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getPatients()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})